GRPC_GATEWAY_DIR := $(shell go list -f '{{ .Dir }}' -m github.com/grpc-ecosystem/grpc-gateway 2> /dev/null)
GRPC_GEN_DOC_DIR := $(shell which protoc-gen-doc)
GO_INSTALLED := $(shell which go)
PROTOC_INSTALLED := $(shell which protoc)
BINDATA_INSTALLED := $(shell which go-bindata 2> /dev/null)
PGGG_INSTALLED := $(shell which protoc-gen-grpc-gateway 2> /dev/null)
PGG_INSTALLED := $(shell which protoc-gen-go 2> /dev/null)
PGD_INSTALLED := $(shell which protoc-gen-doc 2> /dev/null)

# Go parameters
GOCMD=GO111MODULE=on go
GOBUILD=$(GOCMD) build
GODOWNLOAD=$(GOCMD) mod download


install-tools:
ifndef PROTOC_INSTALLED
	$(error "go is not installed, please run 'brew install go'")
endif
ifndef PROTOC_INSTALLED
	$(error "protoc is not installed, please run 'brew install protobuf'")
endif
ifndef BINDATA_INSTALLED
	@go get -u github.com/kevinburke/go-bindata/go-bindata@master
endif
ifndef PGGG_INSTALLED
	@go get -u github.com/grpc-ecosystem/grpc-gateway/...
endif
ifndef PGG_INSTALLED
	@go get -u github.com/golang/protobuf/protoc-gen-go
endif
ifndef PGD_INSTALLED
	@go get -u github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
endif


generate: install-tools
	@protoc \
		-I/usr/local/include \
		-I. \
		-I$(GRPC_GATEWAY_DIR)/third_party/googleapis \
		--go_out=plugins=grpc:api \
		--grpc-gateway_out=logtostderr=true:api \
		--proto_path api api.proto \
		--plugin=protoc-gen-doc=$(GRPC_GEN_DOC_DIR) \
		--doc_opt=html,index.html \
		--doc_out=./doc

download:
	$(GODOWNLOAD)

build:
	rm -rf target
	mkdir target
	cp cmd/config-example.toml target/config.toml
	$(GOBUILD) -o target/bilinadmin ./main.go

clean:
	rm -rf target

test:
	go test -v -cover ./...

dev:
	go run main.go -c=cmd/config-example.toml

run:
    # 启动 bilinadmin 应用
	target/bilinadmin -c=target/config.toml

run2:
    # 启动 bilinadmin 应用
	nohup target/bilinadmin -c=target/config.toml 2>&1 > target/bilinadmin.log &

stop:
    # 关闭 bilinadmin 应用
	pkill -f target/bilinadmin

proto-old:
	protoc -I=. -I=$(GOPATH)/src --gofast_out=plugins=grpc:./api/pb/server ./api/proto/server/*.proto
	protoc -I=. -I=$(GOPATH)/src --gofast_out=plugins=grpc:./api/pb/client ./api/proto/client/account.proto
	protoc -I=. -I=$(GOPATH)/src --gofast_out=plugins=grpc:./api/pb/client ./api/proto/client/bilin_open.proto
	sed -i "" -e "s/,omitempty//g" ./api/pb/*/*.go

proto:
	protoc -I=. --gofast_out=plugins=grpc:./api/pb/server ./api/proto/server/*.proto
	protoc -I=. --gofast_out=plugins=grpc:./api/pb/client ./api/proto/client/account.proto
	protoc -I=. --gofast_out=plugins=grpc:./api/pb/client ./api/proto/client/bilin_open.proto
	sed -i "" -e "s/,omitempty//g" ./api/pb/*/*.go